﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SeaBattle.UI.Web.Models;
using SeaBattle.Domain.Abstract.Services;
using SeaBattle.Domain.Abstract;
using SeaBattle.Domain.Entities;

namespace SeaBattle.UI.Web.Controllers
{
    [Authorize]
    public class ProfileController : Controller
    {
        private IPlayerService playerService;
        private IGameService gameService;
        private IAccountService accountService;
        private IGameRepository gameRepository;

        public ProfileController(IPlayerService playerService, IGameService gameService, IAccountService accountService,
                                IGameRepository gameRepository)
        {
            this.playerService = playerService;
            this.gameService = gameService;
            this.accountService = accountService;
            this.gameRepository = gameRepository;
        }
        //
        // GET: /Profile/

        public ActionResult Index(string login)
        {
           var player = playerService.GetPlayer(login);
           var user = playerService.GetPlayer(User.Identity.Name);
           // Ask the account for the balance value
           var balance = accountService.GetPlayerBalance(user);
           // Get game list
           var gameList = gameService.GetOpenedGames().Where(x => x.Creator == player);
           var closedGameList = gameRepository.Games.Where(x => x.State == GameState.STATE_CLOSED)
                                                    .Where(x => (x.Creator == player || x.Opponent == player));
            return View(new ProfileViewModel { Player = player, GameList = gameList,PlayerAccountBalance=balance,
                                                ClosedGameList = closedGameList});
        }

    }
}
